--------------------------------------------------------------------------------------
-- 插入最下面最后一个节点
--@relative_type :1 为下级最后一个节点；2：是相对节点左边节点
--------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[AddSubNodeEnd]
(
  @table_name VARCHAR(50),
  @relative_node_id int,
	@node_id int,
	@node_name varchar(50),
	@relative_type INT
)
AS
declare @rgt int
if exists(select Node_id from Tree where Node_id = @node_id)
	begin
		SET XACT_ABORT ON
		BEGIN TRANSCTION
      select @rgt = Rgt from Tree where Node_id = @node_id
      update Tree set Rgt = Rgt + 2 where Rgt >= @rgt
      update Tree set Lft = Lft + 2 where Lft >= @rgt
      insert into Tree(Name, Lft, Rgt) values(@node_name, @rgt, @rgt + 1)
      COMMIT TRANSACTION
      SET XACT_ABORT OFF
    END
	END
GO


